<!-- 首页 -->
<template>
	<view>
		<!-- 搜索框 -->
		<u-gap height="10" bgColor="#ffffff"></u-gap>
		<u-search :showAction="true" actionText="搜索" placeholder="请输入想搜索的新闻" v-model="keyword" :animation="true"
			@custom="handleClickSearh"></u-search>
		<u-gap height="10" bgColor="#ffffff"></u-gap>
		<!-- 轮播图 -->
		<u-swiper :list="list3" indicator indicatorMode="line" circular></u-swiper>
		<!-- 通告 -->
		<u-notice-bar :text="text1"></u-notice-bar>
		<!-- 选项列表 -->
		<u-tabs :list="list1" @click="handleTypeclick"></u-tabs>
		<!-- 新闻列表 -->

		<u-list :show-scrollbar="true" v-if="status!='loading'" height="400px">
			<u-list-item v-for="(item, index) in indexList" :key="index">
				<u-cell :title="item.news_title" @click="handleClickItem(item.news_id)">
					<u-avatar slot="icon" shape="square" size="35" :src="item.news_cover"
						customStyle="margin: -3px 5px -3px 0"></u-avatar>
				</u-cell>
			</u-list-item>
			<u-gap height="10" bgColor="#ffffff"></u-gap>
			<u-loadmore :status="status" :loading-text="loadingText" :loadmore-text="loadmoreText"
				:nomore-text="nomoreText" />
		</u-list>
		<u-loadmore v-if="status==='loading'" :status="status" :loading-text="loadingText" :loadmore-text="loadmoreText"
			:nomore-text="nomoreText" />
	</view>
</template>

<script>
	import api from "../../common/api/api.js"
	export default {
		data() {
			return {
				list3: [
					'https://cdn.uviewui.com/uview/swiper/swiper3.png',
					'https://cdn.uviewui.com/uview/swiper/swiper2.png',
					'https://cdn.uviewui.com/uview/swiper/swiper1.png',
				],
				text1: '这一波“高考表情”值得定格',
				list1: [{
					name: '全部'
				}, {
					name: '军事'
				}, {
					name: '政治'
				}, {
					name: '财经'
				}, {
					name: '体育'
				}, {
					name: '娱乐'
				}, {
					name: '科技'
				}, {
					name: '教育'
				}, {
					name: '医疗'
				}],
				indexList: [],
				keyword: '',
				status: 'nomore',
				loadingText: '努力加载中',
				loadmoreText: '轻轻上拉',
				nomoreText: '实在没有了'
			}
		},
		async onLoad() {
			// 页面加载获取数据
			// uni.request({
			// 	url: "https://unidemo.dcloud.net.cn/api/news",
			// 	method: "GET",
			// 	success: (res) => {
			// 		this.indexList = res.data
			// 	}
			// })
			uni.removeStorageSync("userInfo")
			this.status = "loading"
			let newData = await api("/news/news_selectAll")
			this.indexList = newData.data
			this.status = "nomore"
			console.log(this.indexList)

		},
		methods: {
			// 携带参数去详情页面
			handleClickItem(e) {
				uni.navigateTo({
					url: "/pages/home/detail/detail?titleId=" + e
				})
			},
			// 携带参数去搜索页面
			handleClickSearh(v) {
				uni.navigateTo({
					url: "/pages/home/searchDetail/searchDetail?selectMsg=" + v
				})
			},
			async handleTypeclick(type) {
				this.status = "loading"
				// 全部数据
				console.log(type)
				if (type.name == "全部") {
					let newData = await api("/news/news_selectAll")
					this.indexList = newData.data
					console.log(this.indexList)
					return
				}
				// 类型数据
				let newData = await api("/news/selectByType?news_type=" + type.name)
				this.indexList = newData.data
				this.status = "nomore"
				console.log(this.indexList)
			}
		},
	}
</script>